﻿package pluginfw.nio
{
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;
    import morn.core.handlers.*;
    import pluginfw.nio.filters.*;
    import pluginfw.nio.imsg.*;

    public class Transport extends EventDispatcher
    {
        protected var socket:Socket;
        public var host:String;
        public var port:int;
        public var conenctSucHandler:Handler;
        private var _connectTimerid:uint = 0;
        protected var filter:BaseFilter;

        public function Transport()
        {
            this.filter = new BaseFilter();
            return;
        }// end function

        public function isConnect() : Boolean
        {
            return this.socket && this.socket.connected;
        }// end function

        public function setFilter(param1:BaseFilter) : void
        {
            this.filter = param1;
            return;
        }// end function

        public function getFilter() : BaseFilter
        {
            return this.filter;
        }// end function

        private function clearTimer() : void
        {
            if (this._connectTimerid > 0)
            {
                clearTimeout(this._connectTimerid);
                this._connectTimerid = 0;
            }
            return;
        }// end function

        public function connect(param1:String, param2:int) : void
        {
            this.clearTimer();
            this.host = param1;
            this.port = param2;
            if (this.socket == null)
            {
                this.socket = new Socket();
                this.socket.addEventListener(Event.CLOSE, this.closeHandler);
                this.socket.addEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
                this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
                this.socket.addEventListener(Event.CONNECT, this.connectHandler);
                this.socket.addEventListener(ProgressEvent.SOCKET_DATA, this.receivedHandler);
            }
            this.socket.connect(param1, param2);
            return;
        }// end function

        public function connectDelay(param1:int, param2:String, param3:int) : void
        {
            this.clearTimer();
            this.host = param2;
            this.port = param3;
            setTimeout(this.connect, param1, param2, param3);
            return;
        }// end function

        private function closeHandler(event:Event) : void
        {
            this.destroy();
            this.dispatchEvent(event);
            return;
        }// end function

        private function ioErrorHandler(event:IOErrorEvent) : void
        {
            this.filter.handleDisconnect(this.socket);
            this.dispatchEvent(event);
            return;
        }// end function

        private function securityErrorHandler(event:SecurityErrorEvent) : void
        {
            this.filter.handleDisconnect(this.socket);
            this.dispatchEvent(event);
            return;
        }// end function

        private function connectHandler(event:Event) : void
        {
            this.filter.handleConnect(this.socket);
            this.dispatchEvent(event);
            return;
        }// end function

        private function receivedHandler(event:ProgressEvent) : void
        {
            this.filter.handleRead(this.socket);
            return;
        }// end function

        public function disconnect() : void
        {
            this.filter.handleDisconnect(this.socket);
            return;
        }// end function

        public function destroy() : void
        {
            if (this.conenctSucHandler)
            {
                this.conenctSucHandler.dispose();
                this.conenctSucHandler = null;
            }
            this.clearTimer();
            this.socket.removeEventListener(Event.CLOSE, this.closeHandler);
            this.socket.removeEventListener(IOErrorEvent.IO_ERROR, this.ioErrorHandler);
            this.socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
            this.socket.removeEventListener(Event.CONNECT, this.connectHandler);
            this.socket.removeEventListener(ProgressEvent.SOCKET_DATA, this.receivedHandler);
            return;
        }// end function

        public function sendMessage(param1:IMessage) : void
        {
            if (this.filter != null)
            {
                this.filter.handleWrite(param1, this.socket);
            }
            return;
        }// end function

        public function sendDirectMsg(param1:String) : void
        {
            this.socket.writeUTFBytes(param1);
            this.socket.flush();
            return;
        }// end function

    }
}
